//
//  XDPersonViewController.m
//  Le way walkers
//
//  Created by POP-mac on 15/5/13.
//  Copyright (c) 2015年 LTXZ. All rights reserved.
//

#import "XDPersonViewController.h"
#import "XDPersonCollectViewController.h"
#import "UIImageView+WebCache.h"
#import "DKNightVersion.h"
#import "XDRecommendDateBaseManager.h"
#import "UMSocial.h"
#import "XDPerson1TableViewCell.h"
#import "XDPerson.h"

@interface XDPersonViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *Arr;
@property (nonatomic, retain) UITableViewCell *cell;
@property (nonatomic, retain) NSDictionary *dic;
@property (nonatomic, assign) BOOL chose;
@property (nonatomic, retain) XDRecommendDateBaseManager *dbManager;
@property (nonatomic, retain) NSString *str;
@property (nonatomic, retain) UILabel *QClable;
@property (nonatomic, retain) UIView *login_view;
@property (nonatomic, assign) BOOL login;
@property (nonatomic, retain) UIButton *sina;
@property (nonatomic, retain) UIButton *tp;
@property (nonatomic, retain) UIImageView *image;
@property (nonatomic, retain) UIImageView *image_false;
@property (nonatomic, retain) UIImageView *Image_1;
@end

@implementation XDPersonViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationController.navigationBar.nightBarTintColor = [UIColor colorWithRed:0.196 green:0.201 blue:0.212 alpha:1.0];
    
    self.navigationController.navigationBar.hidden = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"请登录";
    self.Arr = [@[@"收藏",@"夜间模式",@"清除缓存",@"关于我们"]mutableCopy];
    [self createTableView];
    
    self.login_view = [[UIView alloc] init];
    
    
    
    [self.login_view setFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 100)];
//    self.login_view.hidden = YES;
    [self.login_view setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.2]];
    [self.view addSubview:self.login_view];
    [self.login_view release];
    
    UIImageView *close = [[UIImageView alloc] init];
    [close setFrame:CGRectMake(self.view.frame.size.width - 40, 10, 30, 30)];
    close.image = [UIImage imageNamed:@"close"];
    [close setUserInteractionEnabled:YES];
    UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)] autorelease];
    [close addGestureRecognizer:tap];
    [self.login_view addSubview:close];
    [close release];
    
    self.sina = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.sina setFrame:CGRectMake(20, 10, 55, 77)];
    [self.sina setBackgroundImage:[UIImage imageNamed:@"im_login_more_sns_sina_press"] forState:UIControlStateNormal];
    [self.sina addTarget:self action:@selector(sinaTap) forControlEvents:UIControlEventTouchUpInside];
    [self.login_view addSubview:self.sina];
    
    self.tp = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.tp setFrame:CGRectMake(self.sina.frame.origin.x + 70 + 30, 10, 55, 77)];
    [self.tp setBackgroundImage:[UIImage imageNamed:@"im_login_more_sns_tencent_press"] forState:UIControlStateNormal];
    [self.tp addTarget:self action:@selector(tpTap) forControlEvents:UIControlEventTouchUpInside];
    [self.login_view addSubview:self.tp];
}


- (void)sinaTap
{
    NSLog(@"sina");
    
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            [self success_login];
        }});
    
}

- (void)success_login
{
    NSLog(@"denglu");
    
    [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToSina  completion:^(UMSocialResponseEntity *response){
        NSLog(@"SnsInformation is %@",response.data);
        [self.dic setValuesForKeysWithDictionary:response.data];
        self.dic = response.data;
        NSLog(@"%@", self.dic);
        self.navigationItem.title = [self.dic objectForKey:@"screen_name"];
        [self.image sd_setImageWithURL:[self.dic objectForKey:@"profile_image_url"]];
        self.login_view.hidden = YES;
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注销" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
    }];
    
}

- (void)tpTap
{
    NSLog(@"tp");
    
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToTencent];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToTencent];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            [self success_login_tp];
        }});
    
}

- (void)success_login_tp
{
    NSLog(@"denglu");
    
    [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToTencent  completion:^(UMSocialResponseEntity *response){
        NSLog(@"SnsInformation is %@",response.data);
        [self.dic setValuesForKeysWithDictionary:response.data];
        self.dic = response.data;
        NSLog(@"%@", self.dic);
        self.navigationItem.title = [self.dic objectForKey:@"screen_name"];
        [self.image sd_setImageWithURL:[self.dic objectForKey:@"profile_image_url"]];
        self.login_view.hidden = YES;
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注销" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
    }];
}

- (void)viewWillDisappear:(BOOL)animated
{
//    [self.view removeFromSuperview];
    self.login = YES;
    [self tap];
}

//视图即将出现
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSInteger size = [[SDImageCache sharedImageCache] getSize];
    float flSize = size /1024.0 /1024.0;
    self.str = [NSString stringWithFormat:@"%.2fM",flSize];
    _QClable.text = self.str;
    
    if ([self.navigationItem.title isEqualToString:@"请登录"]) {
        [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToSina  completion:^(UMSocialResponseEntity *response){
            NSLog(@"%@", response.data);
            
            if (response.data.allKeys.count <= 3) {
                NSLog(@"没有");
                return;
            }
            else
            {
                self.navigationItem.title = [response.data objectForKey:@"screen_name"];
                [self.image sd_setImageWithURL:[response.data objectForKey:@"profile_image_url"]];
                self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注销" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
                self.login_view.hidden = YES;
            }
        }];
        NSLog(@"正在获取");
    }
    
    if ([self.navigationItem.title isEqualToString:@"请登录"]) {
        [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToTencent  completion:^(UMSocialResponseEntity *response){
            NSLog(@"%@", response.data);
            
            if (response.data.allKeys.count <= 3) {
                NSLog(@"没有");
                return;
            }
            else
            {
                self.navigationItem.title = [response.data objectForKey:@"screen_name"];
                [self.image sd_setImageWithURL:[response.data objectForKey:@"profile_image_url"]];
                self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注销" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
                self.login_view.hidden = YES;
            }
        }];
        NSLog(@"正在获取");
    }
    
    else
    {
        NSLog(@"youle");
    }
    
}

-(void)createTableView
{
    self.tableView = [[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49 - 40) style:UITableViewStyleGrouped]autorelease];
    _tableView.delegate = self;
    _tableView.dataSource =self;
    _tableView.rowHeight = 50;
    [self.view addSubview:_tableView];
    
    UISwitch *swith = [[UISwitch alloc] initWithFrame:CGRectMake(self.tableView.frame.size.width - 70, 260, 0, 0)];
    swith.on = self.chose;
    [swith addTarget:self action:@selector(swith_tap:) forControlEvents:UIControlEventValueChanged];
    [self.tableView addSubview:swith];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
    
    //UIView *view = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 155)]autorelease];

    self.Image_1 = [[[UIImageView alloc]init]autorelease];
    self.Image_1.frame = CGRectMake(0, 0, self.view.frame.size.width, 200);
    self.Image_1.image = [UIImage imageNamed:@"e207422e8052f946020b0b20b3100fc9.jpg"];
    _tableView.tableHeaderView = self.Image_1;
    
    
    //_tableView.tableHeaderView = view;
//    UIView *view1 = [[[UIView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 115)]autorelease];
//    view1.backgroundColor = [UIColor colorWithRed:255/255.0 green:251/255.0 blue:240/255.0 alpha:1];
    //开启用户交互
    self.Image_1.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    [self.Image_1 addGestureRecognizer:tap];
    //[view addSubview:view1];
    //label.backgroundColor = [UIColor orangeColor];
    
    self.image_false = [[UIImageView alloc] init];
    self.image_false.image = [UIImage imageNamed:@"d19fc5c72e147b43b8e423f276bbd26b.jpg"];
    [self.image_false setFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    [self.Image_1 addSubview:self.image_false];
    [self.Image_1 release];
    
    self.image = [[[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 60 , self.image_false.frame.size.height - 160, 120, 120)]autorelease];
    [self.image_false addSubview:self.image];
    [self.image setImage:[UIImage imageNamed:@"login_back_pink"]];
    [self.image.layer setCornerRadius:60];
    self.image.layer.masksToBounds = YES;
    self.QClable = [[UILabel alloc]initWithFrame:CGRectMake(self.tableView.frame.size.width - 65, 260 + 45, 60, 40)];
    [_tableView addSubview:_QClable];
    _QClable.textColor = [UIColor grayColor];
    [_QClable release];

    NSInteger size = [[SDImageCache sharedImageCache] getSize];
    float flSize = size /1024.0 /1024.0;
    self.str = [NSString stringWithFormat:@"%.2fM",flSize];
    _QClable.text = self.str;
   
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat yy = scrollView.contentOffset.y;
    NSLog(@"%f",yy);
    if (yy <= 0) {
        yy = 0 - yy;
        self.image_false.frame = CGRectMake(0 - yy, 0 - yy, self.view.frame.size.width + yy + yy, 200 + yy);
        self.image.frame = CGRectMake(self.image_false.frame.size.width / 2 - 60 , self.image_false.frame.size.height - 160, 120, 120);
    }
    
    
    
}


- (void)swith_tap:(UISwitch *)tagg
{
    NSLog(@"kaikai");
    self.chose = tagg.on;
    if (tagg.on) {
        self.view.window.alpha = 0.618;
//        [DKNightVersionManager nightFalling];
    }else{
//        [DKNightVersionManager dawnComing];
        self.view.window.alpha = 1;
    }
    
}

- (void)tap
{
//    self.login_view.hidden = !self.login_view.hidden;
    self.login = !self.login;
    if (self.login) {
        [UIView animateWithDuration:0.5 animations:^{
            [self.login_view setFrame:CGRectMake(0, self.view.frame.size.height - 100, self.view.frame.size.width, 100)];
        }];
        NSLog(@"1");
    }
    else
    {
        [UIView animateWithDuration:0.5 animations:^{
            [self.login_view setFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 100)];
        }];
        NSLog(@"2");
    }
}

- (void)cancel
{
    [[UMSocialDataService defaultDataService] requestUnOauthWithType:UMShareToSina  completion:^(UMSocialResponseEntity *response){
        NSLog(@"response is %@",response);
        self.login_view.hidden = NO;
        self.login = YES;
        [self tap];
//        self.title = @"请登录";
        self.navigationItem.title = @"请登录";
        [self.image setImage:[UIImage imageNamed:@"login_back_pink"]];
    }];
    
    [[UMSocialDataService defaultDataService] requestUnOauthWithType:UMShareToTencent  completion:^(UMSocialResponseEntity *response){
        NSLog(@"response is %@",response);
        self.login_view.hidden = NO;
        self.login = YES;
        [self tap];
//        self.title = @"请登录";
        self.navigationItem.title = @"请登录";
        [self.image setImage:[UIImage imageNamed:@"login_back_pink"]];
    }];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XDPerson1TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (!cell) {
        cell = [[XDPerson1TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
    }
    XDPerson *xd = [[XDPerson alloc] init];
    xd.text = self.Arr[indexPath.row];
    if (indexPath.row == 0) {
        xd.lala = @"iconfont-shoucang (5)";
        xd.papa = @"iconfont-jiantou";
    }else if (indexPath.row == 1){
        xd.lala = @"iconfont-yejian";
    }else if (indexPath.row == 2){
        xd.lala = @"iconfont-458qingchu";
    }else if (indexPath.row == 3){
        xd.lala = @"iconfont-guanyu";
        xd.papa = @"iconfont-jiantou";
    }
    cell.xdper = xd;
    return cell;
}




-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.row == 0) {
        XDPersonCollectViewController *collect = [[XDPersonCollectViewController alloc]init];
        [self.navigationController pushViewController:collect animated:YES];
    }else if (indexPath.row == 2){
        

        
//        
//        //清除磁盘缓存
//        [[SDImageCache sharedImageCache] clearDisk];
//        //清除内存缓存
//        [[[SDWebImageManager sharedManager] imageCache] clearMemory];
//        //清除系统缓存
//        [[NSURLCache sharedURLCache] removeAllCachedResponses];
//        NSInteger size = [[SDImageCache sharedImageCache] getSize];
//        float flSize = size /1024.0 /1024.0;
//        self.str = [NSString stringWithFormat:@"%.2fM",flSize];
//        
//        _QClable.text = self.str;
        
        
        UIAlertView *aview = [[UIAlertView alloc] initWithTitle:@"共有缓存" message:self.str delegate:self cancelButtonTitle:@"返回" otherButtonTitles:@"清除", nil];
        [aview show];
        [aview release];
        

        
    }else if (indexPath.row == 3){
        UIAlertView *al = [[UIAlertView alloc]initWithTitle:@"乐途开发团队" message:@"徐晓东,蒋振,班梦\n2015.5.11~2015.5.29" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [al show];
        [al release];
    }
    
}




//确定的下标为1 取消的下标为0
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if ( buttonIndex == 1) {
        
        //清除磁盘缓存
        [[SDImageCache sharedImageCache] clearDisk];
        //清除内存缓存
        [[[SDWebImageManager sharedManager] imageCache] clearMemory];
        //清除系统缓存
        [[NSURLCache sharedURLCache] removeAllCachedResponses];
        NSInteger size = [[SDImageCache sharedImageCache] getSize];
        float flSize = size /1024.0 /1024.0;
        self.str = [NSString stringWithFormat:@"%.2fM",flSize];
        
        _QClable.text = self.str;

    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
